<?php
include("../config.php");
include("../include/db_connect.php");
include("../include/session.php");
include("../include/common.php");
include("../include/search.php");
include("../include/item.php");

if(isset($_SESSION['user_id'])) {
	$userInfo = getUserInfo($_SESSION['user_id']);
	
	if(isset($_REQUEST['action'])) {
		if ($_REQUEST['action'] == "add") {
			if(isset($_REQUEST['feature'])) {
				$result = postItem($_SESSION['user_id'], $_REQUEST['item_name'], $_REQUEST['item_desc'], $_REQUEST['category_id'], $_REQUEST['cost'], $_REQUEST['method']);
				$message = "";
				if($result === true ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Item Posted!", "message" => $message));
				} else if ($result == -1 ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Error! field left blank", "message" => $message));
				} else if ($result == -2 ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Error! description too short", "message" => $message));
				} else if ($result == -3 ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Error! category does not exist", "message" => $message));
				} else if ($result == -4 ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Error! cost is out of bounds", "message" => $message));
				} else if ($result == -5 ) {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Error! description too long", "message" => $message));
				} else {
					get_page_advanced("message","com", array("name" => $userInfo[0], "title" => "Internal Error! try again", "message" => $message));
				}
			} else {
				get_page_advanced("add","item", array("name" => $userInfo[0]));
			}
		} else if ($_REQUEST['action'] == "view") {
			$items = getItems($_SESSION['user_id']);
			get_page_advanced("view","item", array("name" => $userInfo[0], "items" => $items));
		}
	} else {
		get_page("message", array("redirect" => "../index.php"));
	}
	
} else {
	get_page("message", array("redirect" => "../index.php"));
}
?>
